package com.jiayang.hhotel.ui;

import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.jiayang.hhotel.R;
import com.jiayang.hhotel.fragment.FrontFragment;
import com.jiayang.hhotel.fragment.WoDeFragment;
import com.jiayang.hhotel.util.ToastUtil;
import com.jiayang.hhotel.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/8 0008.
 * 布局框架
 */
public class FrontActivity extends XActivity implements BottomNavigationBar.OnTabSelectedListener {

    private CustomViewPager pager;

    @Override
    public void initData(Bundle savedInstanceState) {
        initView();
    }

    private void initView(){
        pager = (CustomViewPager) findViewById(R.id.fragment_container);

        FrontFragment frontFragment = new FrontFragment();
        FrontFragment feedBackFragment = new FrontFragment();
        FrontFragment messageFragment = new FrontFragment();
        WoDeFragment woDeFragment = new WoDeFragment();

        List<Fragment> list = new ArrayList<>();

        list.add(frontFragment);
        list.add(feedBackFragment);
        list.add(messageFragment);
        list.add(woDeFragment);

        //设置不可滑动
        pager.setScrollable(false);

        pager.setOffscreenPageLimit(4);
        pager.setAdapter(new XFragmentAdapter(getSupportFragmentManager(),list,null));

        BottomNavigationBar bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar
                .setActiveColor("#FFFFFF")//
                .setInActiveColor("#FFFFFF")
                .setBarBackgroundColor("#222222");
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.shouye, "首页").setActiveColor(Color.parseColor("#e68e29")))
                .addItem(new BottomNavigationItem(R.mipmap.tousujianyi, "投诉建议").setActiveColor(Color.parseColor("#e68e29")))
                .addItem(new BottomNavigationItem(R.mipmap.xiaoxi_bt, "消息").setActiveColor(Color.parseColor("#e68e29")))
                .addItem(new BottomNavigationItem(R.mipmap.wode_bottom, "我的").setActiveColor(Color.parseColor("#e68e29")))
                .initialise();
        bottomNavigationBar.setTabSelectedListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout .activity_front;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void onTabSelected(int position) {
        switch (position){
            case 0:
                pager.setCurrentItem(0,false);
                break;
            case 1:
                pager.setCurrentItem(1,false);
                break;
            case 2:
                pager.setCurrentItem(2,false);
                break;
            case 3:
                pager.setCurrentItem(3,false);
                break;
        }
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    boolean isExit;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Timer tExit;
            if (!isExit) {
                isExit = true;
                ToastUtil.Toast(getString(R.string.press_exit),this);
                tExit = new Timer();
                tExit.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isExit = false;
                    }
                }, 2000);
            } else {
                System.exit(0);
            }
        }
        return false;
    }
}
